<?php
/**
 * 博客热门标签组件
 * 用于展示博客中的热门标签列表
 */
$configKey = isset($configKey) ? $configKey : 'blog-tags';
// 从页面配置获取博客热门标签组件配置，使用默认值作为后备
$data = array_merge([
    'section_id' => 'blog-tags',
    'section_pretitle' => '',
    'section_title' => '热门标签',
    'section_subtitle' => '查看常用标签，发现更多相关内容',
    'bg_color' => 'white',
    'border_radius' => 'xl',
    'shadow' => 'sm',
    'padding' => '6',
    'margin_bottom' => '8',
    'title_margin_bottom' => '4',
    'gap' => '2',
    'tag_default_bg' => 'gray-100',
    'tag_default_text' => 'gray-700',
    'tag_hover_bg' => 'primary',
    'tag_hover_text' => 'white',
    'popular_tags' => [
        ['name' => 'JavaScript', 'count' => 32],
        ['name' => 'React', 'count' => 28],
        ['name' => 'UI设计', 'count' => 21],
        ['name' => '性能优化', 'count' => 19],
        ['name' => '移动开发', 'count' => 15],
        ['name' => 'CSS', 'count' => 14],
        ['name' => '用户体验', 'count' => 12]
    ]
], $pageConfig['components'][$configKey] ?? []);

// 处理激活标签和分类
$activeTag = $_GET['tag'] ?? '';
$activeCategory = $_GET['category'] ?? 'all';
$currentPage = $_GET['page'] ?? 1;

// 构建URL参数
$urlParams = [];
if (!empty($activeCategory) && $activeCategory !== 'all') {
    $urlParams[] = 'category=' . $activeCategory;
}
if (!empty($currentPage) && $currentPage > 1) {
    $urlParams[] = 'page=' . $currentPage;
}
?>

<!-- 热门标签 -->
<div id="<?= $data['section_id'] ?>" class="bg-<?= $data['bg_color'] ?> rounded-<?= $data['border_radius'] ?> shadow-<?= $data['shadow'] ?> p-<?= $data['padding'] ?> mb-<?= $data['margin_bottom'] ?>">
    <h3 class="text-xl font-bold mb-<?= $data['title_margin_bottom'] ?>">
        <?= $data['section_title'] ?>
    </h3>
    <div class="flex flex-wrap gap-<?= $data['gap'] ?>">
        <?php foreach ($data['popular_tags'] as $tag): ?>
            <?php
            // 构建标签链接
            $tagParams = $urlParams;
            array_unshift($tagParams, 'tag=' . urlencode($tag['name']));
            $tagQuery = '?' . implode('&', $tagParams);
            // 检查是否为激活标签
            $isActiveTag = ($activeTag === $tag['name']);
            $tagClass = $isActiveTag ? 
                "tag-pill bg-{$data['tag_hover_bg']} text-{$data['tag_hover_text']}" : 
                "tag-pill bg-{$data['tag_default_bg']} text-{$data['tag_default_text']} hover:bg-{$data['tag_hover_bg']} hover:text-{$data['tag_hover_text']}";
            ?>
            <a href="/blog.php<?= $tagQuery ?>" class="<?= $tagClass ?>">
                <?= htmlspecialchars($tag['name']) ?>
                <span class="ml-1 opacity-80">(<?= $tag['count'] ?>)</span>
            </a>
        <?php endforeach; ?>
    </div>
</div>